package com.chb.utils;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @program: gz01-springmvc01
 * @description:
 * @author: 闫克起2
 * @create: 2023-09-18 12:00
 **/
public class OssUtils {
    private static String endpoint = "https://oss-cn-guangzhou.aliyuncs.com";
    private static String accessKeyId = "LTAI5tMXjzW4ZoV4FuZtQaYA";
    private static String accessKeySecret = "8XgZ79YqHmzpMe50EZS2cnsmKjN3dI";
    private static String bucketName = "web-chb";
    public static String ossUpload(MultipartFile multipartFile) {
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        //  上传的文件名称
        String fileName = "images/" + UUID.randomUUID().toString().replace("-", "") + multipartFile.getOriginalFilename();
        try {
            ossClient.putObject(bucketName, fileName, multipartFile.getInputStream());
        } catch (OSSException | ClientException | IOException oe) {
            oe.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        //  文件访问路径
        return endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;
    }


    /**
     * oss文件删除
     *
     * @param pictureUrl 图片地址
     * @return 返回上传后的oss路径
     */
    public static void ossRemove(String pictureUrl) {
        String[] segments = pictureUrl.split("/");
        String filename = segments[segments.length - 1];


        // 填写文件完整路径。文件完整路径中不能包含Bucket名称。
        String objectName = filename;

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            // 删除文件或目录。如果要删除目录，目录必须为空。
            ossClient.deleteObject(bucketName, objectName);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

}